import vue from '@vitejs/plugin-vue';
import type { PluginOption } from 'vite';
import legacy from '@vitejs/plugin-legacy';
import vueJsx from '@vitejs/plugin-vue-jsx';
// eslint-disable-next-line @typescript-eslint/no-var-requires
const mock = require('../../build/mock/createMockServer');
import createSvgIcon from './svg-icon';
import createCompression from './compression';
import createAutoImport from './auto-import';
// 弃用该插件，原因是导致public资源无法访问
// import createPluginHtml from './plugin-html';
import { visualizer } from 'rollup-plugin-visualizer';
export default function createVitePlugins(env, isBuild) {
  const vitePlugins: (PluginOption | PluginOption[])[] = [
    legacy({
      // 项目中可按实际生产环境的浏览器类型及版本来制定兼容性要求
      targets: ['chrome >= 90'],
      // 如需兼容较低版本浏览器，需要开启polyfill和legecyChunks
      modernPolyfills: false,
      renderLegacyChunks: false
    }),
    vue(),
    vueJsx(),
    mock({
      watch: true,
      mockUrlList: [/api/],
      cwd: process.cwd(),
      enable: env.VITE_HTTP_MOCK === 'true' && process.env.NODE_ENV !== 'production'
    }),
    process.argv.includes('--report') &&
      (visualizer({
        filename: 'bundle-stats.html', // 输出文件名
        open: true // 在默认用户代理中打开生成的文件
      }) as PluginOption)
  ];
  vitePlugins.push(createAutoImport());
  vitePlugins.push(createSvgIcon());
  // vitePlugins.push(createPluginHtml(env, isBuild));
  isBuild && vitePlugins.push(...createCompression(env));
  return vitePlugins;
}
